package co.com.javeriana.aes.oms.products;

import co.com.javeriana.aes.oms.common.BaseBean;
import co.com.javeriana.aes.oms.common.entity.Campaign;
import co.com.javeriana.aes.oms.constants.NavRules;
import co.com.javeriana.aes.touresbalon.webserviceclients.ProductsClient;
import co.com.javeriana.aes.touresbalon.webserviceclients.Search;
import co.com.javeriana.aes.touresbalon.webserviceclients.dto.ProductDTO;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author Jonathan Fernandez <jonathan.fvargas@gmail.com>
 */
@ManagedBean(name = "ProductBean")
@RequestScoped
public class ProductBean extends BaseBean implements java.io.Serializable{

//    @EJB
//    private CommonServiceLocal localService;    
    private Campaign selected;
    private UploadedFile file;
    private Integer idContry;
    private String contry;
    private ProductDTO ProductDTO = new ProductDTO();
    private List <Integer> top5Productos = new ArrayList<Integer>();
    private String orderID;
   
    //private Map<Integer, String> nombreMap = new HashMap<Integer, String>();
    
    //private List<Campaign> dtos;

    @PostConstruct
    public void init() {
      //  nombreMap = CitiesClient.getCities();
        
        //dtos = localService.getCampaigns();
    }

//    public StreamedContent getImage() {
//
//        FacesContext context = FacesContext.getCurrentInstance();
//
//        if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
//            // So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
//            return new DefaultStreamedContent();
//        } else {
//            // So, browser is requesting the image. Return a real StreamedContent with the image bytes.
//            String id = context.getExternalContext().getRequestParameterMap().get("id");
//            Campaign c = null;
//            try {
//                c = localService.getCampaign(Long.valueOf(id));
//            } catch (Exception ex) {
//                ex.printStackTrace();
//            }
//
//            return new DefaultStreamedContent(new ByteArrayInputStream(c.getImage()));
//        }
//    }

    public String getOrderID() {
        return orderID;
    }

    public void setOrderID(String orderID) {
        this.orderID = orderID;
    }
    
    public List<Integer> getTop5Productos() {
        return top5Productos;
    }

    public void setTop5Productos(List<Integer> top5Productos) {
        this.top5Productos = top5Productos;
    }
    

    public String getContry() {
        return contry;
    }

    public void setContry(String contry) {
        this.contry = contry;
    }

    public Integer getIdContry() {
        return idContry;
    }

    public void setIdContry(Integer idContry) {
        this.idContry = idContry;
    }
    
    public Campaign getSelected() {
        return selected;
    }

    public void setSelected(Campaign selected) {
        this.selected = selected;
    }

//    public Map<Integer, String> getNombreMap() {
//        return nombreMap;
//    }
//
//    public void setNombreMap(Map<Integer, String> nombreMap) {
//        this.nombreMap = nombreMap;
//    }
    
//
//    public List<Campaign> getDtos() {
//        return dtos;
//    }
//
//    public void setDtos(List<Campaign> dtos) {
//        this.dtos = dtos;
//    }

    public ProductDTO getProductDTO() {
        return ProductDTO;
    }

    public void setProductDTO(ProductDTO ProductDTO) {
        this.ProductDTO = ProductDTO;
    }

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

    private void resetValues() {
        this.ProductDTO = new ProductDTO();
    }

    public String cancelButton() {
        return NavRules.GO_TO_HOME.name();
    }

    public void saveButton() {
        
        try {
            if (file != null) {                
                ProductDTO.setImage(file.getContents());
                
                //calling service  
                Integer prodId = ProductsClient.addProduct(ProductDTO);
                resetValues();
                
                addMessage("Producto No "+prodId+" creado con éxito");
                
            } else {
                addMessage("Error creando producto");
            }

        } catch (Exception ex) {
            addMessage("Error creando prodcuto");
            Logger.getLogger(ProductBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void onCountryChange() {
    
        System.err.println(this.idContry);
        
    }
    
    
    public void top5() {
        
        try {

            if (orderID.isEmpty()) {
                init();
            } else {
                
                this.top5Productos = Search.getTop5Products( Integer.valueOf(orderID) );

                if (top5Productos.isEmpty()) {
                    addMessage("No existen productos asociados");
                    //dtos.add(selected);
                }
            }

        } catch (Exception e) {
            //show message no info
            addMessage("ocurrio un error desconocido");
        }
    }
    
    
    public String deleteEvent() {
        try {
            //delete object
            //localService.deleteCampaign(selected);
            addMessage("Producto eliminado con éxito");
        } catch (Exception ex) {
            addMessage("Error al eliminar producto");
            ex.printStackTrace();
        }
        return null;
    }    

}
